package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.VO.PhAdminVO;
import com.student.VO.PhAdminVOs;
import com.student.VO.UserVO;
import com.student.dto.ResponseDto;
import com.student.mapper.SnackAdminMapper;
import com.student.pojo.PhRole;
import com.student.pojo.SnackAdmin;
import com.student.pojo.User;
import com.student.service.SnackAdminService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: makeups
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2024-12-18 23:03
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SnackAdminServiceImpl implements SnackAdminService {
    @Autowired
    private SnackAdminMapper snackAdminMapper;

    @Override
    public ResponseDto adminLogin(UserVO phUserVO) {
        SnackAdmin snackAdmin = snackAdminMapper.adminLogin(phUserVO);
        if (snackAdmin != null) {
            return ResponseDto.success(snackAdmin);
        }
        return  new ResponseDto(2,"账号或者密码出错",null);
    }
//  渲染用户信息
    @Override
    public ResponseDto insertPagination(PhAdminVO phAdminVO) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(phAdminVO.getCurrentPage(), phAdminVO.getPageSize());
        List<User> phAdminDO = snackAdminMapper.selectPagination(phAdminVO);

        PageInfo<User> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto updateStatics(Integer status, Integer id) {
        Integer res = snackAdminMapper.updateStatics(status, id);

        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto resUserPassword(Integer id) {
        Integer res = snackAdminMapper.resUserPassword(id);
        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteUser(Integer id) {
        Integer res = snackAdminMapper.deleteUser(id);
        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }
//  这边是业务处理层  处理管理信息渲染
    @Override
    public ResponseDto adminInsertPage(PhAdminVO phAdminVO) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(phAdminVO.getCurrentPage(), phAdminVO.getPageSize());
        List<SnackAdmin> phAdminDO = snackAdminMapper.adminInsertPage(phAdminVO);

        PageInfo<SnackAdmin> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto getRoleName() {
        List<PhRole> allRole= snackAdminMapper.getRoleName();


        return ResponseDto.success(allRole);
    }

    @Override
    public ResponseDto updateAdminRoleId(Integer roleId, Integer id) {
        Integer res = snackAdminMapper.updateAdminRoleId(roleId, id);
        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteAdmin(Integer id) {
        Integer res = snackAdminMapper.deleteAdmin(id);
        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addAdminAccountAndPassword(PhAdminVOs phAdminVOs) {
        SnackAdmin snackAdmin = snackAdminMapper.adminLogins(phAdminVOs);
        if (snackAdmin != null) {
            return new ResponseDto(2,"账号已存在,请重新添加",null);
        }

        Integer res = snackAdminMapper.addAdminAccountAndPassword(phAdminVOs);

        return ResponseDto.success(null);
    }

    @Override
    public ResponseDto restPassword(Integer id, String oldPassword, String newPassword) {
        SnackAdmin snackAdmin = snackAdminMapper.selectPassword(id,oldPassword);
        if (snackAdmin != null) {
         Integer res=snackAdminMapper.restPassword(id,newPassword);
         if (res>0){
             return ResponseDto.success(null);
         }
        }else {
            return new ResponseDto(2,"旧密码有误",null);
        }
        return ResponseDto.error(null);
    }
}